<?php
// +----------------------------------------------------------------------
// | 火凤凰CMS内容管理系统
// +----------------------------------------------------------------------
// | Copyright (c) 2015~2018 http://cxbs.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 赵志广 <amdin@cxbs.net>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\common\controller\Adminbase;
use app\common\model\Order as cxModel;
use weixinhack\WxBase;

class Order extends Adminbase {
    protected $beforeActionList = [
        'btnyz'  =>  ['only'=>'index,so,allindex,huodong,solist'],
    ];
    protected function btnyz(){
        $btn = $this->btnauth('edit,del');
        $this->assign('btn',$btn);
    }
    //  订单速览
    public function index(){
        $cxmodel = new cxModel();
        // 获取前一天订单信息
        $parttime['jinadd'] = strtotime(date("Y-m-d 00:00:00",time()));
        $parttime['twoadd'] = time()-86400;
        $parttime['jinend'] = time();
        $parttime['zuoadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-1 day")));
        $parttime['zuoend'] = strtotime(date("Y-m-d 23:59:59",strtotime("-1 day")));
        $parttime['swadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-3 day")));
        $parttime['zhouadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-7 day")));
        $parttime['yueadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-30 day")));
        //  产品订单量
        $order['shopjinnum'] = $cxmodel->where('mid','103')->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();
        $order['shopzuonum'] = $cxmodel->where('mid','103')->where('addtime','>',$parttime['zuoadd'])->where('addtime','<',$parttime['zuoend'])->count();
        //  课程订单量
        $order['kechengjinnum'] = $cxmodel->where('mid','100')->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();
        $order['kechengzuonum'] = $cxmodel->where('mid','100')->where('addtime','>',$parttime['zuoadd'])->where('addtime','<',$parttime['zuoend'])->count();
        //  特约订单量
        $order['yuejinnum'] = $cxmodel->where('mid','1001')->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();
        //  违约金订单量
        $order['endjinnum'] = $cxmodel->where('mid','1002')->where('addtime','>',$parttime['zuoadd'])->where('addtime','<',$parttime['zuoend'])->count();
        //  充值订单量
        $order['chongjinnum'] = $cxmodel->where('mid','1003')->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();
        //  保证金订单量
        $order['baojinnum'] = $cxmodel->where('mid','1004')->where('addtime','>',$parttime['jinadd'])->where('addtime','<',$parttime['jinend'])->count();

        //  收入统计
        $order['zuomon'] = $cxmodel->where('addtime','>',$parttime['zuoadd'])->where('addtime','<',$parttime['zuoend'])->where('playstatus','in','1,2,5')->sum('pricemoney');
        $order['swmon'] = $cxmodel->where('addtime','>',$parttime['swadd'])->where('addtime','<',$parttime['zuoend'])->where('playstatus','in','1,2,5')->sum('pricemoney');
        $order['zhoumon'] = $cxmodel->where('addtime','>',$parttime['zhouadd'])->where('addtime','<',$parttime['zuoend'])->where('playstatus','in','1,2,5')->sum('pricemoney');
        $order['yuemon'] = $cxmodel->where('addtime','>',$parttime['yueadd'])->where('addtime','<',$parttime['zuoend'])->where('playstatus','in','1,2,5')->sum('pricemoney');
        //  未付款订单
        $order['playstatus0'] = $cxmodel->where('addtime','>',$parttime['yueadd'])->where('addtime','<',$parttime['twoadd'])->where('playstatus','in','0,4')->count();
        $order['playstatus1'] = $cxmodel->where('mid','103')->where('playstatus','1')->count();
        $order['playstatus2'] = $cxmodel->where('playstatus','2')->count();
        $order['playstatus6'] = $cxmodel->where('playstatus','6')->count();
        //  获取最新订单列表
        $orderlist = $cxmodel->order('addtime desc')->paginate(20)->each(function($item,$key){
            $item->conf = unserialize($item->conf);
        });
        $neworderlist = null;
        if(!empty($orderlist)){
            foreach ($orderlist as $k => $v){
                $v = $v->toArray();
                $neworderlist[$k] = $v;
            }
        }
        $order['orderlist'] = $neworderlist;
        $this->assign([
            'order' => $order,
            'thistime' => time(),
            'mid' => '',
        ]);
        return view();
    }
    //  全部订单
    public function allindex(){
        $cxmodel = new cxModel();
        $getdata = request()->param();
        if(empty($getdata['mid']) || $getdata['mid'] == 'all'){
            $getdata['mid'] = 'all';
            $map['mid'] = array('<>','null');
        }else{
            $map['mid'] = $getdata['mid'];
        }
        $orderlist = $cxmodel->where($map)->order('addtime desc')->paginate(20);
        $pages = $orderlist->render();
        $neworderlist = null;
        if(!empty($orderlist)){
            foreach ($orderlist as $k => $v){
                $v = $v->toArray();
                $v['conf'] = unserialize($v['conf']);
                $neworderlist[$k] = $v;
            }
        }
        $orderlist = $neworderlist;
        $this->assign([
            'orderlist' => $orderlist,
            'thistime' => time(),
            'pages' => $pages,
            'mid' => $getdata['mid'],
        ]);
        return view();
    }

    //  编辑订单
    public function edit(){
        $cxmodel = new cxModel();
        $data = request()->param();
        $edit = $cxmodel->where('oid',$data['oid'])->find();
        $edit['conf'] = unserialize($edit['conf']);
        $edit['orderplay'] = model("OrderPlay")->where('oid',$edit['oid'])->find();
        if(request()->isPost()){
            $data = input('post.');
            $data = datatrim($data);
            if($data['ordersave'] == '1'){
                $data['playstatus'] = '2';
            }
            if($data['ordersave'] == '2'){
                $data['playstatus'] = '6';
            }
//            elseif($data['ordersave'] == '2'){
//                $wxbase = new WxBase();
//                $nesdata = array_merge($edit->toArray(),$data);
//                $nesdata['tpricemoney'] = $data['tpricemoney'] * 100;
//                $nesdata['oid'] = 'TK'.date("Ymd").$this->addoid();
//                $tuikuan = $wxbase->get_tuikuan($this->webdb,$nesdata);
//                if($tuikuan['return_code'] == 'SUCCESS'){
//                    if($tuikuan['result_code'] == 'SUCCESS'){
//                        $newdata = array(
//                            'oid' => $tuikuan['out_refund_no'],
//                            'uid' => $edit['uid'],
//                            'mid' => '0',
//                            'title' => $edit['oid'].'退款订单',
//                            'uphone' => $edit['uphone'],
//                            'yunyingshang' => $edit['yunyingshang'],
//                            'diqu' => $edit['diqu'],
//                            'allmoney' => $edit['pricemoney'],
//                            'pricemoney' => $tuikuan['refund_fee'],
//                            'playstatus' => '4',
//                            'addtime' => time(),
//                            'conf' => serialize($tuikuan),
//                            'playtime' => time(),
//                        );
//                        $this->addorder($newdata);
//                    }else{
//                        $this->error($tuikuan['err_code_des']);
//                    }
//                }
//                $data['playstatus'] = '6';
//            }

            if($cxmodel->allowField(true)->isUpdate(true)->save($data,['oid'=>$data['oid']])){
                if($data['ordersave'] == '1'){
                    $this->success("订单已发货！");
                }
            }else{
                $this->error("订单处理失败");
            }
        }
        $edit['userdata'] = model('UserData')->where('uid',$edit['uid'])->find()->toArray();
        $kuaidilist = model('express')->where('class','0')->order('id ASC')->select();
        $this->assign([
            'postdb' => $edit,
            'kuaidilist' => $kuaidilist,
            'thistime' => time(),
        ]);
        return view();
    }
//
    public function addorder($data){
        $cxmodel = new cxModel();
        $newdata = $cxmodel->where('oid',$data['oid'])->value('title');
        if(empty($newdata)){
            if($cxmodel->allowField(true)->save($data)){
                return true;
            }
        }else{
            if($cxmodel->allowField(true)->isUpdate(true)->save($data)){
                return true;
            }
        }
        return false;
    }
    // 生成订单编号
    protected function addoid(){
        $numbers = range (1,50);
        shuffle ($numbers);
        $num=8;
        $result = array_slice($numbers,0,$num);
        $result = implode('',$result);
        $newname = '';
        for ( $i = 0; $i < 8; $i++ ) {
            $newname .= substr($result, mt_rand(0, strlen($result) - 1), 1);
        }
        return $newname;
    }
    //  订单搜索
    public function solist(){
        $cxmodel = new cxModel();
        $data = request()->param();
        $data = datatrim($data);
        if(!empty($data['times']) && isset($data['times'])){
            $val = explode('---',$data['times']);
            foreach ($val as $v){
                $vals[] = trim($v);
            }
            $start = strtotime($vals[0]);
            $end =  strtotime(date($vals[1])) + 86400;
            $sodata['addtime'] = array(array('>=',$start),array('<=',$end));
        }
        if(!empty($data['mid'])) {
            switch ($data['mid']) {
                case 'all':
                    $sodata['mid'] = array('<>','null');
                    break;
                default:
                    $sodata['mid'] = $data['mid'];
                    break;
            }
        }
        if(empty($data['mid'])){
            $data['mid'] = 'all';
        }
        if(!empty($data['app'])){
            // 获取前一天订单信息
            $parttime['jinadd'] = strtotime(date("Y-m-d 00:00:00",time()));
            $parttime['twoadd'] = time()-86400;
            $parttime['jinend'] = time();
            $parttime['zuoadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-1 day")));
            $parttime['zuoend'] = strtotime(date("Y-m-d 23:59:59",strtotime("-1 day")));
            $parttime['swadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-3 day")));
            $parttime['zhouadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-7 day")));
            $parttime['yueadd'] = strtotime(date("Y-m-d 00:00:00",strtotime("-30 day")));
            switch ($data['app']) {
                case '1':
                    $sodata['addtime'] = array(array('>',$parttime['jinadd']),array('<',$parttime['jinend']));
                    break;
                case '2':
                    $sodata['addtime'] = array(array('>',$parttime['zuoadd']),array('<',$parttime['zuoend']));
                    break;
                case '3':
                    $sodata['addtime'] = array(array('>',$parttime['jinadd']),array('<',$parttime['jinend']));
                    $sodata['pricemoney'] = array('<>','0');
                    break;
                case '4':
                    $sodata['addtime'] = array(array('>',$parttime['jinadd']),array('<',$parttime['jinend']));
                    $sodata['pricemoney'] = '0';
                    break;
            }
        }
        if(!empty($data['playstatus']) || $data['playstatus'] == '0') {
            switch ($data['playstatus']) {
                case 'all':
                    break;
                case '0':
                    $sodata['playstatus'] = array('in', '0,4');
                    break;
                case '1':
                    $sodata['playstatus'] = '1';
                    break;
                case '2':
                    $sodata['playstatus'] = '2';
                    break;
                case '3':
                    $sodata['playstatus'] = '3';
                    break;
                case '5':
                    $sodata['playstatus'] = '5';
                    break;
                case '6':
                    $sodata['playstatus'] = '6';
                    break;
            }
        }
        if(!empty($data['keyword']) && isset($data['keyword'])) {
            $sodata['oid|title|uphone|conf']=array('like',"%{$data['keyword']}%");
        }
        if(empty($data['keys'])){
            $data['keys'] = 'allindex';
        }
        $list = $cxmodel->where($sodata)->order('addtime desc')->paginate(30,false,['query' => request()->param()])->each(function ($item){
            $item->conf = unserialize($item->conf);
        });
        $neworderlist = null;
        $pages = $list->render();
        if(!empty($list)){
            foreach ($list as $k => $v){
                $v = $v->toArray();
                $neworderlist[$k] = $v;
            }
        }
        $orderlist = $neworderlist;
        $this->assign([
            'orderlist' => $orderlist,
            'so' => $data,
            'pages' => $pages,
            'thistime' => time(),
            'mid' => $data['mid'],
        ]);
        return view('allindex');
    }
    //  删除
    public function del(){
        if(!request()->param('id')){
            $this->error('访问错误！');
        }
        if(request()->isPost()){
            $cxmodel = new cxModel();
            $deldata = cxModel::get(request()->param('id'));
            $title = $deldata['title'];
            if($deldata->delete()){
                model('OrderPlay')->where('oid',$deldata)->delete();
                $this->addlog("删除订单【{$title}】成功！");
                $this->success("删除订单【{$title}】成功！",'index');
            }else{
                $this->error("删除订单【{$title}】失败！");
            }
        }
        return;
    }
    public function pdel(){
        if(request()->isPost()){
            $cxmodel = new cxModel();
            $data = input('post.');
            $data = datatrim($data);
            foreach ($data['pdel'] as $v){
                $deldata = $cxmodel->where('oid',$v)->find();
                $title = $deldata['title'];
                model('OrderPlay')->where('oid',$v)->delete();
                $deldata->delete();
                $this->addlog("删除订单【{$title}】成功！");
            }
            $this->success("批量删除通知成功！");
        }
        return;
    }
}
